#include "CocosJoy.h"
#include "CocosHelper.h"

using namespace cocos2d;

void CCocosJoy::onEnter()
{
	CCLayer::onEnter();
	CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0);
}

void CCocosJoy::onExit()
{
	CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}

bool CCocosJoy::IsContainsTouch(CCTouch *pTouch)
{
	return (CCRect::CCRectContainsPoint(m_rcCapture,convertTouchToNodeSpaceAR(pTouch)));
}

void CCocosJoy::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) 
{
	if(!m_bEnable || m_bTouched)
	{
		return;
	}
	if(!pTouches||!pTouches->count())
		return;
	CCTouch* pTouch = (CCTouch*)(*(pTouches->begin()));

	m_pCurTouch = pTouch;
	//if(IsContainsTouch(pTouch))
	{
		m_bTouched = true;

		if(m_bCanMove)
		{
			m_ptCenter = convertTouchToNodeSpace(pTouch);
			m_pSprBack->stopAllActions();
			m_pSprBack->setOpacity(255);
			m_pSprBack->setPosition(m_ptCenter);
			m_pSprBack->setIsVisible(true);
			m_pSprCenter->stopAllActions();
			m_pSprCenter->setOpacity(255);
			m_pSprCenter->setIsVisible(true);
			m_pSprCenter->setPosition(m_ptCenter);
			m_curRotation = -1;
			m_curPower    = 0;
			
		}
	}
}

void CCocosJoy::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) 
{
	if(!m_bEnable)
	{
		return;
	}
	if(!pTouches||!pTouches->count())
		return;

	CCSetIterator iter;
	for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
	{
		if(m_pCurTouch == *iter)
		{
			break;;
		}
	}

	if(iter == pTouches->end())
	{
		return;
	}

	if(m_bTouched)
	{
		CCPoint ptCur = convertTouchToNodeSpace((CCTouch *)(*iter));

		float rotation = CMathHelper::CalRotation(m_ptCenter, m_ptCur);
		float dist     = cocos2d::ccpDistance(ptCur, m_ptCenter);
		if(dist < JOY_MOVERAIDUS_MAX)
		{
			m_pSprCenter->setPosition(ptCur);
		}
		else
		{
			dist = JOY_MOVERAIDUS_MAX;
			CCPoint ptInc = CMathHelper::CalIncrement(m_ptCenter, ptCur, JOY_MOVERAIDUS_MAX);
			m_pSprCenter->setPosition(ccpAdd(m_ptCenter, ptInc));
		}

		m_curRotation = rotation;
		m_curPower    = dist / JOY_MOVERAIDUS_MAX;
	}
}

void CCocosJoy::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) 
{
	if(!m_bEnable || !m_bTouched)
	{
		return;
	}
	if(!pTouches||!pTouches->count())
		return;
	CCSetIterator iter;
	for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
	{
		if(m_pCurTouch == *iter)
		{
			if(m_bTouched)
			{
				m_pSprCenter->runAction(CCFadeOut::actionWithDuration(3.0f));
				m_pSprCenter->runAction(CCMoveTo::actionWithDuration(3.0f, m_ptCenter));
				m_pSprBack->runAction(CCFadeOut::actionWithDuration(3.0f));
				m_pCurTouch = NULL;
				m_bTouched = false;
				m_curRotation = -1;
				m_curPower    = 0;
			}
		}
	}
}

void CCocosJoy::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) 
{
	ccTouchesEnded(pTouches, pEvent); 
}

CCocosJoy::CCocosJoy()
	: m_pSprBack(NULL)
	, m_pSprCenter(NULL)
	, m_bCanMove(false)
	, m_bTouched(false)
	, m_bEnable(false)
{

}
void CCocosJoy::InitState(const char *szJoyPath, const char *szJoybkPath, bool isCanMove,  cocos2d::CCRect rcCapture)
{
	m_pSprBack = CCSprite::spriteWithFile(szJoybkPath);
	m_pSprCenter = CCSprite::spriteWithFile(szJoyPath);

	if(isCanMove)
	{
		m_pSprBack->setIsVisible(false);
		m_pSprCenter->setIsVisible(false);
	}

	m_rcCapture = rcCapture;

	addChild(m_pSprBack);
	addChild(m_pSprCenter);

	m_bEnable = true;
	m_bCanMove = isCanMove;

}
void CCocosJoy::SetEnable(bool bEnable)
{
	if(bEnable)
	{

	}
	else
	{

	}


	m_bEnable = bEnable;
}